package org.rsbot.script.randoms;

import java.awt.Point;
import java.awt.Rectangle;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Account;
import org.rsbot.script.methods.Bank;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSPlayer;

@ScriptManifest(authors={"Fred", "Arbiter"}, name="Improved Rewards Box", version=1.3D)
public class ImprovedRewardsBox extends Random
{
  private static final int BOOK_KNOWLEDGE_ID = 11640;
  private static final int LAMP_ID = 2528;
  private static final int MYSTERY_BOX_ID = 6199;
  private static final int BOX_ID = 14664;
  private static final int BOX_IF = 202;
  private static final int BOX_CONFIRM_IF = 28;
  private static final int BOX_SELECTION_IF = 15;
  private static final int BOX_SCROLLBAR_IF = 24;
  private static final int XP_IF = 134;
  private static final int ATT_ID = 4;
  private static final int AGILITY_ID = 5;
  private static final int HERBLORE_ID = 6;
  private static final int FISHING_ID = 7;
  private static final int THIEVING_ID = 8;
  private static final int RUNECRAFTING_ID = 9;
  private static final int SLAYER_ID = 10;
  private static final int FARMING_ID = 11;
  private static final int MINING_ID = 12;
  private static final int SMITHING_ID = 13;
  private static final int HUNTER_ID = 14;
  private static final int COOKING_ID = 15;
  private static final int FIREMAKING_ID = 16;
  private static final int WOODCUTTING_ID = 17;
  private static final int FLETCHING_ID = 18;
  private static final int CONSTRUCTION_ID = 19;
  private static final int SUMMONING_ID = 20;
  private static final int STRENGTH_ID = 21;
  private static final int RANGED_ID = 22;
  private static final int MAGIC_ID = 23;
  private static final int DEFENCE_ID = 24;
  private static final int HITPOINTS_ID = 25;
  private static final int CRAFTING_ID = 26;
  private static final int PRAYER_ID = 27;
  private static final int DUNGEONEERING_ID = 28;
  private static final int CONFIRM_ID = 2;
  private int scrollbarTopLength;
  private int scrollbarTotalLength;
  private int hiddenScreenHeight;
  private int viewableScreenHeight;
  private int endofselection = 0;
  private int XPSelection;

  public boolean activateCondition()
  {
    if ((this.game.isLoggedIn()) && (!getMyPlayer().isInCombat()) && (!this.bank.isOpen()));
    return cachedInventoryContainedOneOf(new int[] { 14664, 11640, 2528, 6199 });
  }

  private boolean cachedInventoryContainedOneOf(int[] paramArrayOfInt)
  {
    for (RSItem localRSItem : this.inventory.getCachedItems()) {
      for (int n : paramArrayOfInt) {
        if (localRSItem.getID() == n) {
          return true;
        }
      }
    }
    return false;
  }

  public int getActualY(RSComponent paramRSComponent)
  {
    RSComponent[] arrayOfRSComponent1 = this.interfaces.get(202).getComponent(15).getComponents();

    for (int j = 0; j < arrayOfRSComponent1.length; j++) {
      if (arrayOfRSComponent1[j].containsText(":")) {
        this.endofselection = (j - 6);
      }
      if (arrayOfRSComponent1[j].containsText("emote")) {
        this.endofselection = (j - 6);
      }
      if (arrayOfRSComponent1[j].containsText("costume")) {
        this.endofselection = (j - 6);
      }
    }
    this.viewableScreenHeight = (this.interfaces.get(202).getComponent(15).getHeight() - 11);
    j = arrayOfRSComponent1[this.endofselection].getAbsoluteY() + arrayOfRSComponent1[this.endofselection].getHeight() - arrayOfRSComponent1[0].getAbsoluteY();

    this.hiddenScreenHeight = (j - this.viewableScreenHeight);
    int i;
    if (this.hiddenScreenHeight > 0) {
      RSComponent[] arrayOfRSComponent2 = this.interfaces.get(202).getComponent(24).getComponents();

      this.scrollbarTopLength = (arrayOfRSComponent2[1].getAbsoluteY() - arrayOfRSComponent2[0].getAbsoluteY());

      int k = arrayOfRSComponent2[5].getAbsoluteY() - arrayOfRSComponent2[3].getAbsoluteY() + arrayOfRSComponent2[3].getHeight() - 6;

      this.scrollbarTotalLength = (this.scrollbarTopLength + k);
      double d = Double.parseDouble(Integer.toString(this.scrollbarTopLength)) / Double.parseDouble(Integer.toString(this.scrollbarTotalLength)) * Double.parseDouble(Integer.toString(this.hiddenScreenHeight));

      i = paramRSComponent.getAbsoluteY() - (int)d;
    } else {
      i = paramRSComponent.getAbsoluteY();
    }
    return i;
  }

  public Rectangle getBoxArea(RSComponent paramRSComponent) {
    return new Rectangle(paramRSComponent.getAbsoluteX(), getActualY(paramRSComponent), paramRSComponent.getWidth(), paramRSComponent.getHeight());
  }

  public int loop()
  {
    if (getMyPlayer().isInCombat()) {
      return -1;
    }
    String[] arrayOfString = getChoices();
    if (this.interfaces.get(202).isValid())
    {
      int m;
      for (RSComponent localRSComponent : this.interfaces.get(137).getComponents()) {
        if (arrayOfString[(arrayOfString.length - 1)].equals("Emote")) {
          break;
        }
        if ((!localRSComponent.containsText("You've already unlocked")) || (!localRSComponent.containsText("emotes")) || (localRSComponent.containsText("<col=0000ff>"))) {
          continue;
        }
        for (m = 0; m < arrayOfString.length; m++) {
          if (arrayOfString[m].contains("Emote")) {
            System.arraycopy(arrayOfString, m + 1, arrayOfString, m, arrayOfString.length - 1 - m);
            arrayOfString[(arrayOfString.length - 1)] = "Emote";
            break;
          }
        }
      }

      ??? = this.interfaces.get(202).getComponent(15).getComponents();
      ??? = 999;
      int i1;
      for (Object localObject2 : arrayOfString) {
        for (i1 = 0; i1 < ???.length; i1++) {
          if (!???[i1].getText().toLowerCase().contains(localObject2.toLowerCase()))
            continue;
          ??? = i1 - 6;
          break;
        }

        if (??? != 999) {
          break;
        }
      }
      if (??? == 999) {
        ??? = 0;
      }
      ??? = this.interfaces.get(202).getComponent(24).getComponents();
      if (this.scrollbarTopLength > 0) {
        this.mouse.move(???[1].getAbsoluteX() + random(1, 7), ???[1].getAbsoluteY() + random(0, 20));

        this.mouse.drag((int)this.mouse.getLocation().getX(), (int)this.mouse.getLocation().getY() - this.scrollbarTopLength);
      }

      if (getBoxArea(???[???]).y > 278) {
        this.mouse.move(???[1].getAbsoluteX() + random(1, 7), ???[1].getAbsoluteY() + random(20, 30));

        ??? = (int)(this.mouse.getLocation().getY() + Double.parseDouble(Integer.toString(getBoxArea(???[???]).y + getBoxArea(???[???]).height - ???[0].getAbsoluteY() - this.viewableScreenHeight)) / Double.parseDouble(Integer.toString(this.hiddenScreenHeight)) * Double.parseDouble(Integer.toString(this.scrollbarTotalLength)));

        if (??? - (int)this.mouse.getLocation().getY() > ???[5].getAbsoluteY() - ???[3].getAbsoluteY() + ???[3].getHeight() - 6)
        {
          ??? = (int)this.mouse.getLocation().getY() + (???[5].getAbsoluteY() - ???[3].getAbsoluteY() + ???[3].getHeight() - 6);
        }

        this.mouse.drag((int)this.mouse.getLocation().getX(), ???);
      }
      sleep(random(3000, 4000));
      ??? = this.interfaces.get(202).getComponent(15).getComponents();
      if (???.length > ???) {
        ??? = getBoxArea(???[???]).x + 15;
        m = getBoxArea(???[???]).y + 15;
        int n = getBoxArea(???[???]).width - 30;
        i1 = getBoxArea(???[???]).height - 30;
        this.mouse.move(random(???, ??? + n), random(m, m + i1));

        this.mouse.click(true);
        this.interfaces.get(202).getComponent(28).doClick();
      }
      return random(3000, 4000);
    }
    if (this.interfaces.get(134).isValid()) {
      this.interfaces.get(134).getComponent(this.XPSelection).doClick();
      this.interfaces.get(134).getComponent(2).doClick();
      return random(3000, 4000);
    }
    if (this.inventory.contains(14664)) {
      this.inventory.getItem(new int[] { 14664 }).doAction("Open");
      return random(3000, 4000);
    }
    if (this.inventory.contains(11640)) {
      this.inventory.getItem(new int[] { 11640 }).doAction("Read");
      return random(3000, 4000);
    }
    if (this.inventory.contains(2528)) {
      this.inventory.getItem(new int[] { 2528 }).doAction("Rub");
      return random(3000, 4000);
    }
    if (this.inventory.contains(6199)) {
      this.inventory.getItem(new int[] { 6199 }).doAction("Open");
      return random(3000, 4000);
    }
    return -1;
  }

  private String[] getChoices() {
    String[] arrayOfString = new String[2];
    arrayOfString[0] = "XP Item";
    arrayOfString[1] = "Cash";

    String str = this.account.getPreferredReward();
    if (str.equals("Attack")) {
      this.XPSelection = 4;
    } else if (str.equals("Strength")) {
      this.XPSelection = 21;
    } else if (str.equals("Defence")) {
      this.XPSelection = 24;
    } else if (str.equals("Range")) {
      this.XPSelection = 22;
    } else if (str.equals("Prayer")) {
      this.XPSelection = 27;
    } else if (str.equals("Magic")) {
      this.XPSelection = 23;
    } else if (str.equals("Runecrafting")) {
      this.XPSelection = 9;
    } else if (str.equals("Construction")) {
      this.XPSelection = 19;
    } else if (str.equals("Hitpoints")) {
      this.XPSelection = 25;
    } else if (str.equals("Agility")) {
      this.XPSelection = 5;
    } else if (str.equals("Herblore")) {
      this.XPSelection = 6;
    } else if (str.equals("Thieving")) {
      this.XPSelection = 8;
    } else if (str.equals("Crafting")) {
      this.XPSelection = 26;
    } else if (str.equals("Fletching")) {
      this.XPSelection = 18;
    } else if (str.equals("Slayer")) {
      this.XPSelection = 10;
    } else if (str.equals("Hunter")) {
      this.XPSelection = 14;
    } else if (str.equals("Mining")) {
      this.XPSelection = 12;
    } else if (str.equals("Smithing")) {
      this.XPSelection = 13;
    } else if (str.equals("Fishing")) {
      this.XPSelection = 7;
    } else if (str.equals("Cooking")) {
      this.XPSelection = 15;
    } else if (str.equals("Firemaking")) {
      this.XPSelection = 16;
    } else if (str.equals("Woodcutting")) {
      this.XPSelection = 17;
    } else if (str.equals("Farming")) {
      this.XPSelection = 11;
    } else if (str.equals("Summoning")) {
      this.XPSelection = 20;
    } else if (str.equals("Dungeoneering")) {
      this.XPSelection = 28;
    } else {
      this.XPSelection = 17;
      arrayOfString[0] = this.account.getPreferredReward();
    }
    return arrayOfString;
  }
}